package com.example.emos.api.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.json.JSONUtil;
import com.example.emos.api.common.util.PageUtils;
import com.example.emos.api.common.util.R;
import com.example.emos.api.controller.form.*;
import com.example.emos.api.db.pojo.TbBusiness;
import com.example.emos.api.db.pojo.TbUser;
import com.example.emos.api.service.BusinessService;
import com.example.emos.api.service.TattooService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/business")
@Tag(name = "BusinessController" ,description = "商户管理")
public class BusinessController {
    @Autowired
    private BusinessService businessService;
    @GetMapping("searchAllBusiness")
    @Operation(summary = "查询所有商户")
//    @SaCheckLogin
    public R searchAllBusiness() {
        ArrayList<HashMap> list = businessService.searchAllBusiness();
        return R.ok().put("list", list);
    }

    @PostMapping("/insert")
//    @SaCheckPermission(value = {"ROOT", "USER:INSERT"}, mode = SaMode.OR)
    @Operation(summary = "添加商户")
    public R insert(@Valid @RequestBody InsertBusinessForm form){
        TbBusiness tbBusiness= JSONUtil.parse(form).toBean(TbBusiness.class);
        tbBusiness.setStatus((byte)1);
//        user.setRole(JSONUtil.parseArray(form.getRole()).toString());
        tbBusiness.setCreateTime(new Date());
        int rows=businessService.insertBusiness(tbBusiness);
        return R.ok().put("rows",rows);
    }
    @PostMapping("/updateBusinessByGlobalId")
    @Operation(summary = "更新商户")
    public R updateBusiness(@Valid @RequestBody InsertBusinessForm form){
        TbBusiness tbBusiness= JSONUtil.parse(form).toBean(TbBusiness.class);
        tbBusiness.setStatus((byte)1);
//        user.setRole(JSONUtil.parseArray(form.getRole()).toString());
        tbBusiness.setCreateTime(new Date());
       int row= businessService.updateBusinessByGlobalId(tbBusiness);
        return R.ok().put("rows",row);
    }


    @PostMapping("/insertBusinessMore")
    @Operation(summary = "批量添加商户")
    public R insertBusinessMore(@Valid @RequestBody InsertBusinessMoreForm ff){
      InsertBusinessForm[] forms=  ff.getInsertBusinessForms();
        ArrayList<TbBusiness> tbBusinesses = new ArrayList<>();
        for(InsertBusinessForm form:forms){
            TbBusiness tbBusiness= JSONUtil.parse(form).toBean(TbBusiness.class);
            tbBusinesses.add(tbBusiness);
        }
        int rows=businessService.insertBusinessMore(tbBusinesses);
        return R.ok().put("rows",rows);
    }

    @PostMapping("/searchBusinessByPage")
    @Operation(summary = "查询商家分页记录")
//    @SaCheckLogin
    public R searchBusinessByPage(@Valid @RequestBody SearchBusinessByPageForm form){
        if((form.getStartDate()!=null&&form.getEndDate()==null)||(form.getStartDate()==null&&form.getEndDate()!=null)){
            return R.error("startDate和endDate只能同时为空，或者不为空");
        }
        int page= form.getPage();
        int length= form.getLength();
        int start=(page-1)*length;
        HashMap param= JSONUtil.parse(form).toBean(HashMap.class);
        param.put("start",start);
//        param.put("currentUserId", StpUtil.getLoginIdAsInt());
//        if(!(StpUtil.hasPermission("AMECT:SELECT")||StpUtil.hasPermission("ROOT"))){
//            param.put("userId", StpUtil.getLoginIdAsInt());
//        }
        PageUtils pageUtils=businessService.searchBusinessByPage(param);
        return R.ok().put("page",pageUtils);

    }
}
